package components;

import java.util.Timer;
import java.util.TimerTask;

/**
 * This class implements the Bombs set by the Player or the Computer
 * It extends from the Field class
 */
public class Bomb extends Field{

	/**
	 * Defines the time till explosion
	 */
	public long time;
	
	/**
	 * Defines the Range of the explosion
	 */
	public int range;
	
	/**
	 * Is true if bomb is already exploded, but still inflicts the game
	 */
	public boolean exploded;
	
	/**
	 * Is used to handle time
	 */
	Timer timer = new Timer();
	
	/**
	 * Is a reference to the Player, which placed the bomb
	 */
	public Bomberman bomber;

	/**
	 * Constructor to create a bomb
	 * @param position	The coordinates of this 
	 * @param bomberman	Is a reference to the Player, which placed the bomb
	 * @param time Defines the time till explosion
	 */
	public Bomb(float[] position, final Bomberman bomberman, long time ) {
		super(position, true, true, false, game.Multiplayer.bomb, "bomb");
		this.bomber=bomberman;
		this.range=bomber.range;
		this.time=time;
		this.exploded=false;

		timer.schedule(new TimerTask() {
			public void run() {
				delete();

			};
		}, time);

	}

	/**
	 * Deletes the Bomb and handles the explosion
	 */
	public void delete (){
		if(!this.exploded){

			String type;
			Item itm;
			game.Multiplayer.map.field[Math.round(this.position[1])][Math.round(this.position[0])]=
					new Fire(new float[]{Math.round(this.position[0]),Math.round(this.position[1])}, true, false, true, game.Multiplayer.fire, true, this.bomber.id);

			for(int i =1;i<=this.range;i++){
				if(Math.round(this.position[1])+i<=game.Multiplayer.map.size-1){
					type=game.Multiplayer.map.field[Math.round(this.position[1])+i][Math.round(this.position[0])].type;
					itm=game.Multiplayer.map.field[Math.round(this.position[1])+i][Math.round(this.position[0])].item;

					if(game.Multiplayer.map.field[Math.round(this.position[1])+i][Math.round(this.position[0])].destroy){

						game.Multiplayer.map.field[Math.round(this.position[1])+i][Math.round(this.position[0])].delete();

						if(itm!=null){
							game.Multiplayer.map.field[Math.round(this.position[1])+i][Math.round(this.position[0])]=
									new Fire(new float[]{Math.round(this.position[0]),Math.round(this.position[1])+i}, true, false, true, game.Multiplayer.fire, itm);
						}
						else{
							game.Multiplayer.map.field[Math.round(this.position[1])+i][Math.round(this.position[0])]=
									new Fire(new float[]{Math.round(this.position[0]),Math.round(this.position[1])+i}, true, false, true, game.Multiplayer.fire);
						}
					}if(type.equals("block") || type.equals("bomb") || type.equals("fire")){
						break;
					}

				}
			}
			for(int i =1;i<=this.range;i++){
				if(Math.round(this.position[1])-i>=0){
					type=game.Multiplayer.map.field[Math.round(this.position[1])-i][Math.round(this.position[0])].type;
					itm=game.Multiplayer.map.field[Math.round(this.position[1])-i][Math.round(this.position[0])].item;

					if(game.Multiplayer.map.field[Math.round(this.position[1])-i][Math.round(this.position[0])].destroy){

						game.Multiplayer.map.field[Math.round(this.position[1])-i][Math.round(this.position[0])].delete();

						if(itm!=null){
							game.Multiplayer.map.field[Math.round(this.position[1])-i][Math.round(this.position[0])]=
									new Fire(new float[]{Math.round(this.position[0]),Math.round(this.position[1])-i},  true, false, true, game.Multiplayer.fire, itm);
						}
						else{
							game.Multiplayer.map.field[Math.round(this.position[1])-i][Math.round(this.position[0])]=
									new Fire(new float[]{Math.round(this.position[0]),Math.round(this.position[1])-i},  true, false, true, game.Multiplayer.fire);
						}
					}if(type.equals("block") || type.equals("bomb") || type.equals("fire")){
						break;
					}

				}
			}
			for(int i =1;i<=this.range;i++){
				if(Math.round(this.position[0])+i<=game.Multiplayer.map.size-1){
					type=game.Multiplayer.map.field[Math.round(this.position[1])][Math.round(this.position[0])+i].type;
					itm=game.Multiplayer.map.field[Math.round(this.position[1])][Math.round(this.position[0])+i].item;

					if(game.Multiplayer.map.field[Math.round(this.position[1])][Math.round(this.position[0])+i].destroy){

						game.Multiplayer.map.field[Math.round(this.position[1])][Math.round(this.position[0])+i].delete();

						if(itm!=null){
							game.Multiplayer.map.field[Math.round(this.position[1])][Math.round(this.position[0])+i]=
									new Fire(new float[]{Math.round(this.position[0])+i,Math.round(this.position[1])},  true, false, true, game.Multiplayer.fire,itm);
						}
						else{
							game.Multiplayer.map.field[Math.round(this.position[1])][Math.round(this.position[0])+i]=
									new Fire(new float[]{Math.round(this.position[0])+i,Math.round(this.position[1])},  true, false, true, game.Multiplayer.fire);
						}
					}if(type.equals("block") || type.equals("bomb") || type.equals("fire")){
						break;
					}

				}
			}
			for(int i =1;i<=this.range;i++){
				if(Math.round(this.position[0])-i>=0){
					type=game.Multiplayer.map.field[Math.round(this.position[1])][Math.round(this.position[0])-i].type;
					itm=game.Multiplayer.map.field[Math.round(this.position[1])][Math.round(this.position[0])-i].item;

					if(game.Multiplayer.map.field[Math.round(this.position[1])][Math.round(this.position[0])-i].destroy){

						game.Multiplayer.map.field[Math.round(this.position[1])][Math.round(this.position[0])-i].delete();

						if(itm!=null){
							game.Multiplayer.map.field[Math.round(this.position[1])][Math.round(this.position[0])-i]=
									new Fire(new float[]{Math.round(this.position[0])-i,Math.round(this.position[1])},  true, false, true, game.Multiplayer.fire,itm);

						}
						else{
							game.Multiplayer.map.field[Math.round(this.position[1])][Math.round(this.position[0])-i]=
									new Fire(new float[]{Math.round(this.position[0])-i,Math.round(this.position[1])},  true, false, true, game.Multiplayer.fire);
					}
				}if(type.equals("block") || type.equals("bomb") || type.equals("fire")){
					break;
				}


			}
		}
	}
	this.exploded=!this.exploded;
	this.timer.cancel();


}

}
